



# Random Effect Canton
raw.re.canton <- ranef(model.r)$canton
namesCan <- rownames(raw.re.canton)
raw.re.canton <- cbind(as.numeric(namesCan), raw.re.canton)
re.canton <- rep(0,26)
	for (t in raw.re.canton[,1]){
		re.canton[t] <- raw.re.canton[raw.re.canton[,1]==t,2]
	}

# Random Effect Education
raw.re.educ <- ranef(model.r)$x1.education
namesEduc <- rownames(ranef(model.r)$x1.education)
raw.re.educ <- cbind(as.numeric(namesEduc),raw.re.educ)
re.educ <- rep(0,6)
	for (t in raw.re.educ[,1]){
		re.educ[t] <- raw.re.educ[raw.re.educ[,1]==t,2]
	}
	
# Random Effect Region
raw.re.reg <- ranef(model.r)$region
namesReg <- rownames(raw.re.reg)
raw.re.reg <- cbind(as.numeric(namesReg), raw.re.reg)
re.reg <- rep(0,7)
	for (t in raw.re.reg[,1]){
		re.reg[t] <- raw.re.reg[raw.re.reg[,1]==t,2]
	}

# Random Effect Age
raw.re.age <- ranef(model.r)$x1.age
namesAge <- rownames(raw.re.age)
raw.re.age <- cbind(as.numeric(namesAge), raw.re.age)
re.age <- rep(0,4)
	for (t in raw.re.age[,1]){
		re.age[t] <- raw.re.age[raw.re.age[,1]==t,2]
	}
	
# Random Effect Gender
re.female1 <- ranef(model.r)$x1.gender
re.female <- rep(NA,2)
re.female[1] <- re.female1[[1]][1]
re.female[2] <- re.female1[[1]][2]


# how to read out the census information
education.pointer <- c(rep(1,32),rep(2,32),rep(3,32),rep(4,32),rep(5,32),rep(6,32))		# for each of the 192 SV
age.pointer <- rep(c(1,1,2,2,3,3,4,4),24)												# ditto
woman.pointer <- rep(c(1,2),96)															# ditto

	
# create prediction (only on latent, y_hat_*) for each of the 192 types for each of the simulations
check.vector <- matrix(NA,192)
	for (p in 1:192){
		check.vector[p] <-  re.age[age.pointer[p]] + re.female[woman.pointer[p]] + re.educ[education.pointer[p]]
	}
	
# Read-in context data in small data form (only 26 rows)
data3 <- read.dta("Contextdata1.dta")
jjj <- data.run$JAHR[1]
data3 <- data3[data3$JAHR==jjj,]
X21 <- data3$leftvote_share
X22 <- data3$romkath_share
X23 <- data3$german_share
X24 <- data3$leftvote_share
X25 <- data3$partyL1s
X26 <- data3$partyL2s
X27 <- data3$partyL3s
X28 <- data3$partyL4s
X29 <- data3$partyL5s
X210 <- data3$partyL6s
region.pointer <- data3$region
regional.re <- rep(NA,26)
for (r in 1:26){
	regional.re[r] <- re.reg[region.pointer[r]]
}

mod.list <- list(c(3,2), c(3,2,5,6,7,8,10))
#mod.list <- list(c(3,2,10), c(3,2,5,6,7,8,10))
#mod.list <- list(c(17), c(5,6,7,8,10))


XX <- cbind(X21,X22,X23,X24,X25,X26,X27,X28,X29,X210)
xLx <- cbind(1,XX[,mod.list[[w]]])
#ifelse(w==1, xLx <- as.matrix(rep(1,length(X21))), xLx <- cbind(1,XX[,mod.list[[w]]]))

#ifelse(w==1, pred.cantonal <- as.vector(rep(fixef(model.r),26)) + re.canton + regional.re , pred.cantonal <- xLx%*%fixef(model.r) + re.canton + regional.re)
pred.cantonal <- xLx%*%fixef(model.r) + re.canton + regional.re

##############################################################################################################################




##############################################################################################################################
## Combining the two latent scores in one to then do the creating ideal-type (voter types and cantons) prediction
# (no use of actual census data now)

# container
latent.synthANDcant <- rep(NA,26*192)
# we need again an object telling us which line belongs to which canton
canton.pointer <- rep(999,26*192)
	for (j in 1:26){
		n1 <- min(which(canton.pointer==999))
		n2 <- n1+191
		canton.pointer[n1:n2] <- rep(j,192)
	}
# we do the same for the 192 synthetic voters which will appear 26 times
synth.voter.pointer <- rep(c(1:192),26)
# We take the appropriate cantonal prediction and the appropriate synthetic voter prediction and combine it (all latent)
		for (u in 1:length(latent.synthANDcant)){
			latent.synthANDcant[u] <- pred.cantonal[canton.pointer[u]] + check.vector[synth.voter.pointer[u]]
		}
# Transform latent to predicted probability
pred.prob.synthANDcant <- pnorm(latent.synthANDcant)
pred.vector <- pred.prob.synthANDcant

##############################################################################################################################



##############################################################################################################################
# Do actual prediction by weighting each ideal type by its actual occurrence

# read-in census data
#setwd("/Users/lleemann/Dropbox/Democratic Deficit exchange folder/BfS")
#block <- read.csv("census final BfS 11 2012-- manip v2.0.csv", header = TRUE)
#block <- block[,-13]

# Save the first line of each canton
N <- rep(NA,26)
for (i in 1:26){
	n <- 3+i*2411
	N[i] <- n	
}
N <- c(N,65100)

# create container, voter types * cantons
pull.out.BIG <- matrix(NA,192,26)

# read out canton by canton, creating "pull.out.BIG" (192*26), for every type and every canton a head count
for (k in 1:26){
	eins <- k
	zwei <- k+1
	block.use <- as.matrix(block[(N[eins]):(N[zwei]-1),])
	# here is all the hard work by hand
	source("census_pull_out_command_CH_joint.R")
	pull.out.BIG[,k] <- pull.out
}
# Slice up the Matrix to a Vector
pull.out.vector <- as.vector(pull.out.BIG)

# create subgroup-weighted support (number of types * p_hat)
support.vector <- pull.out.vector * pred.vector
	
# now aggregate over all 192 types per canton and create cantonal predictions
cantonal.support <- rep(NA,26)
	for (d in 0:25){
		eins <- d*192 + 1
		zwei <- (d+1)*192
		drei <- d+1
		cantonal.support[drei] <- sum(support.vector[eins:zwei])/sum(pull.out.vector[eins:zwei])	
	}


prediction <- cantonal.support
#print(w)